package com.yangyang.androidconnect.phoneinfo;

import android.Manifest;
import android.app.Activity;
import android.app.AlertDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.content.pm.PackageManager;

import androidx.core.app.ActivityCompat;
import androidx.core.content.ContextCompat;

import java.util.ArrayList;
import java.util.List;

// PermissionManager.java
public class PermissionManager {
    private static final String TAG = "PermissionManager";

    // 定义所有需要的权限
    public static class Permissions {
        public static final String[] ALL = {
                Manifest.permission.READ_CONTACTS,
                Manifest.permission.READ_CALL_LOG,
                Manifest.permission.READ_SMS
        };

        public static final String[] CONTACTS = {Manifest.permission.READ_CONTACTS};
        public static final String[] CALL_LOG = {Manifest.permission.READ_CALL_LOG};
        public static final String[] SMS = {Manifest.permission.READ_SMS};
    }

    /**
     * 检查权限是否已授予
     */
    public static boolean hasPermissions(Context context, String[] permissions) {
        for (String permission : permissions) {
            if (ContextCompat.checkSelfPermission(context, permission)
                    != PackageManager.PERMISSION_GRANTED) {
                return false;
            }
        }
        return true;
    }

    /**
     * 检查单个权限是否已授予
     */
    public static boolean hasPermission(Context context, String permission) {
        return ContextCompat.checkSelfPermission(context, permission)
                == PackageManager.PERMISSION_GRANTED;
    }

    /**
     * 请求权限
     */
    public static void requestPermissions(Activity activity, String[] permissions, int requestCode) {
        ActivityCompat.requestPermissions(activity, permissions, requestCode);
    }

    /**
     * 处理权限请求结果
     */
    public static boolean handlePermissionResult(int requestCode, String[] permissions,
                                                 int[] grantResults, PermissionCallback callback) {
        boolean allGranted = true;
        List<String> grantedPermissions = new ArrayList<>();
        List<String> deniedPermissions = new ArrayList<>();

        for (int i = 0; i < permissions.length; i++) {
            if (grantResults[i] == PackageManager.PERMISSION_GRANTED) {
                grantedPermissions.add(permissions[i]);
            } else {
                deniedPermissions.add(permissions[i]);
                allGranted = false;
            }
        }

        if (callback != null) {
            callback.onPermissionResult(requestCode, allGranted, grantedPermissions, deniedPermissions);
        }

        return allGranted;
    }

    /**
     * 显示权限说明对话框
     */
    public static void showPermissionRationale(Activity activity, String message,
                                               DialogInterface.OnClickListener listener) {
        new AlertDialog.Builder(activity)
                .setTitle("权限说明")
                .setMessage(message)
                .setPositiveButton("确定", listener)
                .setNegativeButton("取消", null)
                .show();
    }

    public interface PermissionCallback {
        void onPermissionResult(int requestCode, boolean allGranted,
                                List<String> grantedPermissions, List<String> deniedPermissions);
    }
}